package org.swfer.UIC 
{
	import flash.display.DisplayObjectContainer;
	import flash.display.InteractiveObject;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	
	/**
	 * 弹框
	 * 可选部件 close:InteractiveObject, dragSprite:Sprite
	 * 回调事件 show, hide
	 * @author swfer
	 */
	public class Dialog extends Component 
	{
		public static var dialogLayer:DisplayObjectContainer;
		public static const SHOW:String='show';
		public static const HIDE:String='hide';
		
		protected var close:InteractiveObject;
		protected var dragSprite:Sprite;
		
		
		public function Dialog(view:MovieClip) 
		{
			super(view);
		}
		
		/**
		 * 显示弹出框
		 */
		public function show():void
		{
			addEvt();
			dialogLayer.addChild(view);
			executeCallBack(SHOW);
		}
		
		/**
		 * 隐藏弹出框。隐藏事件立马执行。不受动画影响
		 */
		public function hide():void
		{
			dialogLayer.removeChild(view);
			executeCallBack(HIDE);
			
			removeEvt();
		}
		
		override protected function init():void
		{
			close = view.getChildByName('close') as InteractiveObject;
			dragSprite = view.getChildByName('dragSprite') as Sprite;
		}
		
		override protected function addEvt():void
		{
			if (close) close.addEventListener(MouseEvent.CLICK,closeClick);
			if (dragSprite) dragSprite.addEventListener(MouseEvent.MOUSE_DOWN, onDrag);
		}
		
		override protected function removeEvt():void
		{
			if(close) close.removeEventListener(MouseEvent.CLICK,closeClick);
			if (dragSprite)
			{
				dragSprite.removeEventListener(MouseEvent.MOUSE_DOWN, onDrag);
				dragSprite.removeEventListener(MouseEvent.MOUSE_OUT, stopDrag);
				dragSprite.removeEventListener(MouseEvent.MOUSE_UP,stopDrag);
			}
		}
		
		private function closeClick(e:MouseEvent):void 
		{
			hide();
		}
		
		private function onDrag(e:MouseEvent):void 
		{
			dragSprite.addEventListener(MouseEvent.MOUSE_OUT, stopDrag);
			dragSprite.addEventListener(MouseEvent.MOUSE_UP,stopDrag);
			view.startDrag();
		}
		
		private function stopDrag(e:MouseEvent):void 
		{
			dragSprite.removeEventListener(MouseEvent.MOUSE_OUT, stopDrag);
			
			view.stopDrag();
		}
		
	}

}